//
//  FilmViewController.m
//  WeiDuYY
//
//  Created by 王克杨的Mac on 2019/1/23.
//  Copyright © 2019 W ke杨. All rights reserved.
//

#import "FilmViewController.h"
#import "VideoTableViewCell.h"
#import "Model.h"
#import "SDAutoLayout.h"
#import "LgViewController.h"
#import "DCCycleScrollView.h"
#import "YPViewController.h"
#import "WuViewController.h"
#import "SanViewController.h"
#import "Tiaozhuananniu.h"
#import "AFNetworking.h"
#import "YYModel.h"
#import "UserItemCell.h"
#import "UIImageView+WebCache.h"

#define kScreen_Width [UIScreen mainScreen].bounds.size.width
#define kScreen_Height [UIScreen mainScreen].bounds.size.height
#define kCollection_Height 250
#define IdentifierID @"userCellId"
@interface FilmViewController ()<UITableViewDataSource,UITableViewDelegate,DCCycleScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
{
    Model *mod;
}

@property (strong, nonatomic)UICollectionView *userCollection;
@property (strong, nonatomic)NSMutableArray *userArray;
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSArray *datasoure;
@end
static NSString *oj = @"cell";
@implementation FilmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    self.datasoure=[[NSArray alloc]init];
   
    [self prefersStatusBarHidden];
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    
    [self.view addSubview:self.tableView];

  
}
//隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
    return YES;//隐藏为YES，显示为NO
}

//表格位置
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    
    return _tableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
//表格返回分区
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return self.datasoure.count-3;
    }
    
}
-(void)loadData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    NSDictionary *dict = @{@"key":@"67968aeebf1f4e3b6170f7217b6f3cdb"};
    // POST请求
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:@"http://172.17.8.100/movieApi/movie/v1/findHotMovieList?userId=18&sessionId=15320748258726&page=1&count=10" parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        
        self->mod = [Model yy_modelWithJSON:responseObject];
        //        [self.array addObject:self->mod.result.data];
        self.datasoure =mod.result;
        //刷新表格
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

//表格内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:oj];
//w我点击了cell但是我的cell会遮挡住我的视图
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:oj];
    
    
 
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        
                // 首页效果图
//                cell.xiaoguotu.image = [UIImage imageNamed:@"hdpi副本 2"];
                NSArray *imageArr = @[@"ef18d29a6f6f5841f4a2dfdbcfd60a4b826961.jpg@464w_644h_1e_1c",
                                      @"165ecdbfceef6b83ec7dc8f2f6abf7158073369.jpg@464w_644h_1e_1c",
                                      @"ccf73179dcdef8ace80e03c10912c718137991.jpg@464w_644h_1e_1c",
        
                                      ];
                DCCycleScrollView *banner = [DCCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 250) shouldInfiniteLoop:YES imageGroups:imageArr];
        
                banner.autoScrollTimeInterval = 2;
                banner.autoScroll = YES;
                banner.isZoom = YES;
                banner.itemSpace = 0;
                banner.imgCornerRadius = 10;
                banner.itemWidth = self.view.frame.size.width - 200;
                banner.delegate = self;
                [cell addSubview:banner];
        
    }else{
        if (indexPath.row == 0) {
            [cell  addSubview:self.userCollection];
       }
        else if(indexPath.row ==1){
            //[cell  addSubview:self.userCollection];
        }else if (indexPath.row == 2)
        {
          // [cell  addSubview:self.userCollection];
        }
        
    }
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 260;
}
//返回键的按钮的跳转
-(void)buttonone1{
    Tiaozhuananniu * T = [Tiaozhuananniu new];
    T.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:T animated:YES];
}

//隐藏导航条方法
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}
-(void)viewDidDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}
//点击图片的代理
-(void)cycleScrollView:(DCCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    
    if (index == 0) {
        YPViewController *xangqing = [YPViewController new];
        [self.navigationController pushViewController:xangqing animated:YES];
    }
    else if (index == 1){
        WuViewController *xangqing = [WuViewController new];
        [self.navigationController pushViewController:xangqing animated:YES];
    }
    else{
        SanViewController *xangqing = [SanViewController new];
        [self.navigationController pushViewController:xangqing animated:YES];
    }
    NSLog(@"index = %ld",(long)index);
    
    
}
- (NSMutableArray *)userArray{
    if (!_userArray) {
        _userArray = [NSMutableArray array];
    }
    return _userArray;
}
- (UICollectionView *)userCollection{
    if (!_userCollection) {
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
        flowlayout.headerReferenceSize = CGSizeMake(0, 0);
        flowlayout.itemSize = CGSizeMake(160, 250);  //每个的大小
        flowlayout.minimumLineSpacing = 10;  //每个的纵向间距
        flowlayout.minimumInteritemSpacing = 0;  //每个的横向间距
        _userCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 40, kScreen_Width, kCollection_Height) collectionViewLayout:flowlayout];
        _userCollection.delegate = self;
        _userCollection.dataSource = self;
        _userCollection.showsVerticalScrollIndicator = NO;
        _userCollection.showsHorizontalScrollIndicator = NO;
        flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        [_userCollection setBackgroundColor:[UIColor whiteColor]];
        [_userCollection registerClass:[UserItemCell class] forCellWithReuseIdentifier:IdentifierID];
        
    }
    return _userCollection;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.datasoure.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
#pragma mark--横向表格点击跳转页面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"第 %ld 个",(long)indexPath.item);

#pragma mark--横向表格点击跳转页面
    Tiaozhuananniu *tiao = [Tiaozhuananniu new];
//    [self presentViewController:tiao animated:YES completion:nil];
    tiao.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:tiao animated:YES];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UserItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IdentifierID forIndexPath:indexPath];
    ResultModel *model = _datasoure[indexPath.row];
    //cell.user_image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:mdel.imageUrl]]];
    [cell.user_image sd_setImageWithURL:[NSURL URLWithString:model.imageUrl]];
    return cell;
}

#pragma mark - 颜色转图片
- (UIImage *)createImageWithColor:(UIColor*)color{
    CGRect rect = CGRectMake(0.0f,0.0f,1.0f,1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();
    return theImage;
}
@end

